<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/12
 * Time: 8:48
 */

namespace app\portal\controller;
use app\portal\model\ActivityLogModel;
use app\portal\model\ActivityUploadModel;
use app\portal\model\UserOrganizeModel;
use cmf\controller\HomeBaseController;
use app\portal\model\ActivityModel;
use app\portal\model\UserRankModel;
use app\portal\model\ActivityPreLogModel;

use think\Db;

class ActivityController extends HomeBaseController
{
    public function index(){
        $model=new ActivityModel();
        $rank=new UserOrganizeModel();
        $qians_rank=$rank->with('user')->order('duration desc')->limit(15)->select();
        $this->assign('qians_rank',$qians_rank);
        $all_times=Db::name('activity')->count();
        $log=Db::name('activity_log')->count();
        $people_times=$log;
        $all_duration=Db::name('user_organize')->sum('duration');
        $data=['all_times'=>$all_times,'poeple_times'=>$people_times,'all_duration'=>$all_duration];
        $act_show=$model->with('organize')->with('user')->where('show','neq','')->paginate(10); //活动展示
        $act_adv=$model->with('organize')->with('user')->where('end_time','>',time()-60*60*24)->paginate(10);  //活动预告
        $this->assign($data);
        $this->assign('page_show',$act_show->render());
        $this->assign('page_adv',$act_adv->render());
        $this->assign('act_show',$act_show);
        $this->assign('act_adv',$act_adv);
        return $this->fetch(':activity');
    }
    //活动预告详情
    public function act_adv(){
        $model= new ActivityModel();
        $activity_pre_log=new ActivityPreLogModel();
        $id = input('id');
        $data=$model->where('id',$id)->find();
        $users=$activity_pre_log->with('user')->where('act_id',$id)->select();
        $this->assign('users',$users);
        $this->assign('data',$data);
        return $this->fetch(':activityDetail');
    }

    //活动展示详情
    public function act_show(){
        $model= new ActivityModel();
        $activity_log=new ActivityLogModel();
        $id = input('id');
        $data=$model->where('id',$id)->find();
        $users=$activity_log->with('user')->where('act_id',$id)->field('user_id')->select();
        $array=[];
        foreach ($users as $k=>$v){
            $array[$v['user_id']]=$v['user'];
        }
        $this->assign('data',$data);
        $this->assign('users',$array);
        return $this->fetch(':activityNotice');
    }
    //当前活动时间段地图
    public function map(){
        return $this->fetch(':now_map');
    }
    //当前活动时间段地图
//    public function new_map(){
//        return $this->fetch(':new_map');
//    }
    //牵手志愿者当前时间段人员数据
    public function now_map(){
        $id=input('id');
        $log_model = new ActivityLogModel();
        $time=Db::name('activity')->where('id',$id)->value('time');
        $time=json_decode($time);
        $start_time=0;$end_time=0;
        foreach ($time as $k=>$v){
            $start=strtotime($v[0]);
            $end=strtotime($v[1]);
            if(time()>$start&&time()<$end){
                $start_time=$start;
                $end_time=$end;
            }
        }
        $data=$log_model->with('user')->where('act_id',$id)->where('start_time','>',$start_time)->where('start_time','<',$end_time)->select();
        $a=[];$b=[];$name=[];$time=[];$user_point=[];
        $act_point=Db::name('activity')->where('id',$id)->value('point');
        $act_place=Db::name('activity')->where('id',$id)->value('place');
        foreach ($data as $k=>$v){
            $name[]=$v['user']['user_nickname'];
            $time[]=date('H:i',$v['start_time']);
            $user_point[]=explode(',',$v['location']);
        }
        $act_point=json_decode($act_point);
        foreach ($act_point as $k=>$v){
            $a[]=explode(',',$v)[0];
            $b[]=explode(',',$v)[1];
            $act_point[$k]=explode(',',$v);
        }
        $c=(max($a)+min($a))/2;
        $d=(max($b)+min($b))/2;
        $center=[$c,$d];
        $res=['name'=>$name,'center'=>$center,'time'=>$time,'point'=>$user_point,'act_point'=>$act_point,'act_place'=>json_decode($act_place)];
        return json($res);
    }

}